﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Xml;
using System.Windows.Forms;

namespace PWAsker
{
    class xml_handler
    {
        string szFilename;

        public xml_handler(string filename)
        {
            szFilename = filename;
            XmlTextReader xmlReader;

            if (File.Exists(filename) == false)
            {
                if (MessageBox.Show("Configuration-file not found! Should the default one be crated?", "Configfile not found!", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    Debug.WriteLine("Config File does not exist. Creating default");
                    createDefaultFile();
                }
                else 
                {
                    Debug.WriteLine("Config File does not exist. User decided to do nothing about this.");
                }
            }
            xmlReader = new XmlTextReader(filename);
        }

        private void createDefaultFile()
        {
            // Create a new file in C:\\ dir

            XmlTextWriter textWriter = new XmlTextWriter(szFilename, Encoding.UTF8);

            // Opens the document

            textWriter.WriteStartDocument();

            // Write comments
            textWriter.WriteComment("This is PWAsker configuartion file");
            textWriter.WriteComment("regex "+ MainWindow.PW_REGEX +" will be replaced eith the entry made in this application");

            //Start configuration-Element
            textWriter.WriteStartElement("Configuration");

            // Write elements and strings
            textWriter.WriteStartElement("Executable");
            textWriter.WriteString("\"exec tc 1\"");
            textWriter.WriteEndElement();

            textWriter.WriteStartElement("Executable");
            textWriter.WriteString("\"exec tc 2\"");
            textWriter.WriteEndElement();

            textWriter.WriteStartElement("Executable");
            textWriter.WriteString("\"exec keepass\"");
            textWriter.WriteEndElement();

            //End configuration-Element
            textWriter.WriteEndElement();
            
            // Ends the document.
            textWriter.WriteEndDocument();

            // close writer
            textWriter.Close();
        }

        public List<String> getCommands()
        {
            Debug.WriteLine("Reading XML-File: "+ szFilename);
            List<String> lSzRet = new List<String>();

            XmlTextReader textReader = new XmlTextReader(szFilename);
            textReader.Read();

            //marking 'configuration'-section as active
            Boolean ConfNode = false;

            while (textReader.Read())
            {
                textReader.MoveToElement();

                //truggers for 'configuration'-section
                if (textReader.NodeType == XmlNodeType.Element)
                {
                    if (textReader.Name == "Configuration")
                    {
                        ConfNode = true;
                    }
                }
                else if (textReader.NodeType == XmlNodeType.EndElement)
                {
                    if (textReader.Name == "Configuration")
                    {
                        ConfNode = false;
                    }
                }

                if (ConfNode == true)
                {
                    if (textReader.NodeType == XmlNodeType.Element)
                    {
                        if (textReader.Name == "Executable")
                        {
                            if (textReader.Read())
                            {
                                textReader.MoveToElement();
                                if (textReader.NodeType == XmlNodeType.Text)
                                {
                                    Debug.WriteLine("Adding " + textReader.Value + " to returner"); 
                                    lSzRet.Add(textReader.Value);
                                }
                            }
                        }
                    }
                }
                

                Debug.WriteLine("XmlElement dump:");
                Debug.WriteLine("===================");
                Debug.WriteLine("Name:" + textReader.Name);
                Debug.WriteLine("Base URI:" + textReader.BaseURI);
                Debug.WriteLine("Local Name:" + textReader.LocalName);
                Debug.WriteLine("Attribute Count:" + textReader.AttributeCount.ToString());
                Debug.WriteLine("Depth:" + textReader.Depth.ToString());
                Debug.WriteLine("Line Number:" + textReader.LineNumber.ToString());
                Debug.WriteLine("Node Type:" + textReader.NodeType.ToString());
                Debug.WriteLine("Value:" + textReader.Value.ToString());
                Debug.WriteLine("===================");

            }

            Debug.WriteLine("Reading XML-File: " + szFilename + " completed!");
            return lSzRet;
        }

    }
}
